package com.polaris.live.viewmodel.backpack

import androidx.lifecycle.MutableLiveData
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.request
import com.polaris.live.common.network.requestLoading
import com.polaris.live.common.network.state.ResultState
import com.polaris.live.net.userService
import com.polaris.live.resp.back_resp.UseConsumableResp
import com.polaris.live.resp.back_resp.UserBackpackConsumableResp
import com.polaris.live.resp.back_resp.UserConsumableGiveVo
import com.polaris.live.resp.go_resp.BackPackRedRequest
import com.polaris.live.resp.go_resp.UseConsumableRequest
import com.polaris.live.resp.go_resp.UserConsumableGiveRequest

/**
 * BackPackToolViewModel 背包Model
 *
 * @author Created by 天晴 on 2024/1/17 9:15
 * @since 1.0.0
 **/
class BackPackToolViewModel : BaseViewModel() {

    //获取用户的道具列表
    val mConsumableLiveData = MutableLiveData<ResultState<List<UserBackpackConsumableResp>>>()
    fun getConsumableList() {
        request({ userService.getConsumableList() }, mConsumableLiveData)
    }

    //使用道具
    val mUseConsumableLiveData = MutableLiveData<ResultState<UseConsumableResp>>()
    fun useConsumable(
        backpackId: Long,
        otherUserId: Long?,
        back: (data: UseConsumableResp) -> Unit,
    ) {
        val useDressUp = UseConsumableRequest(backpackId, otherUserId, null)
        requestLoading({ userService.useConsumable(useDressUp) }, {
            back.invoke(it)
            mUseConsumableLiveData.value = ResultState.onAppSuccess(it)
        }, {
            mUseConsumableLiveData.value = ResultState.onAppError(it)
        })
    }

    val mPutBackpackNewLiveData = MutableLiveData<ResultState<Any>>()

    /**
     * 更新背包红点
     */
    fun putBackpackNew(backpackType: Long, decorationType: Long? = null) {
        request(
            { userService.putBackpackNew(BackPackRedRequest(backpackType, decorationType)) },
            mPutBackpackNewLiveData
        )
    }

    /**
     * 赠送道具
     */
    val mGiveConsumableLiveData = MutableLiveData<ResultState<UserConsumableGiveVo>>()
    fun giveTool(consumableType: Int, otherUserId: Long) {
        request(
            { userService.giveTool(UserConsumableGiveRequest(consumableType, otherUserId)) },
            mGiveConsumableLiveData
        )
    }
}